/**
 * Copyright (c) 2017-2018, zengjintao (1913188966@qq.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jfast.framework.web.handler;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfast.framework.Jfast;
import com.jfast.framework.aop.InteceptorManager;
import com.jfast.framework.aop.Invocation;
import com.jfast.framework.exception.ResolverException;
import com.jfast.framework.log.Logger;
import com.jfast.framework.web.core.Action;
import com.jfast.framework.web.core.ActionReport;
import com.jfast.framework.web.core.Config;
import com.jfast.framework.web.resolver.ViewManager;
import com.jfast.framework.web.resolver.ViewResolver;

/**
 * @version 1.0
 */
public class ActionHandler extends Handler{

	private static final Logger logger = Logger.getLogger(ActionHandler.class);
	private ActionMapping actionMapping;
	
	private static final Config config = Config.getConfig();
	
	private static final ActionHandler actionHandler = new ActionHandler();
	
	private static final InteceptorManager inteceptorManager = InteceptorManager.getInteceptorManager();
	
	public ActionMapping getActionMapping() {
		return actionMapping;
	}

	public void setActionMapping(ActionMapping actionMapping) {
		this.actionMapping = actionMapping;
	}
	
    private ActionHandler(){
		
	}
    
    public static ActionHandler getActionhandler() {
		return actionHandler;
	}
	
	@Override
	public void doHandler(String target,HttpServletRequest request,HttpServletResponse response){
		Action action = config.getAction(target);
		if(action == null){
			if (logger.isWarnEnabled()) {
				String qs = request.getQueryString();
				logger.warn("404 Action Not Found: " + (qs == null ? target : target + "?" + qs));
			}
		}
		try {
			Object controller = Jfast.getBean(action.getControllerClass());
			Jfast.injectMembers(controller);//guice依赖注入
			Invocation invocation =	new Invocation(action, controller,
					inteceptorManager.getInterceptorsToArray(),request,response);
    		invocation.invoke(request,response);
    		ActionReport.report(target, action,request);
			Object returnValue = invocation.getReturnValue();
			ViewResolver viewResolver = ViewManager.getViewManager().getViewFactory().getViewResolver(returnValue);
			viewResolver.setContext(returnValue, request, response).render();
		}catch(ResolverException e) {
			if (logger.isErrorEnabled()) {
				String qs = request.getQueryString();
				logger.error(qs == null ? target : target + "?" + qs, e);
			}
		}
		catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
}
